package com.chaosj.webmagic.dto;

import lombok.Data;
import lombok.experimental.Accessors;

import java.util.Random;

/**
 * @className: GushiwenDto
 * @description:
 * @author: caoyangjie
 * @date: 2021/8/11
 **/
@Data
@Accessors(chain = true)
public class SearchRequestDto {
    private int type = 0;
    private String cmd = null;
    private String search;
    private Integer page;
    private SearchEnums enums = SearchEnums.HOME;

    public SearchRequestDto setType(int type) {
        this.type = type;
        this.enums = SearchEnums.getInstance(type);
        return this;
    }

    public String holdRequest(String request){
        int rdi = new Random().nextInt(5);
        rdi = rdi <=0 ? 1 : rdi;
        rdi = page==null? rdi : page;
        if( type == 5 || type == 6){
            request = String.format(request, search, rdi);
        }else{
            request = String.format(request, rdi);
        }
        if( type == 4 ) {
            cmd = "type";
            return search==null ? request : request.replace("type=","type="+search);
        }
        if( type == 3 ) {
            return search==null ? request : request.replace("c=","c="+search);
        }
        if( type == 2 || type == 1){
            return search==null ? request : request.replace(cmd+"str=",cmd+"str="+search);
        }
        return request;
    }
}
